package com.technology.ctrm;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
//@Log4j2
public class CtrmController216 {
	private static final Logger logger = LogManager.getLogger(CtrmController216.class);

	/**
	 * 先升级log4j2版本为2.16展示问题,后面升级到2.17.0查看耗时是否减少
	 *
	 * @return
	 */
	@RequestMapping("/log4j216")
	public Map log4j216() {
		// 2.16之前所有版本(不包括2.12.3)存在递归查找导致堆栈溢出而终止线程，请求量大时对正常用户可能无法提供服务。
		final String inputStr = "${${::-${::-$${::-$}}}}";
		long start = System.currentTimeMillis();
		logInput(inputStr);
		return new HashMap() {{
			put("inputStr", inputStr);
			put("cost", (System.currentTimeMillis() - start));
			put("timestamp", LocalDateTime.now());
		}};
	}

	private static void logInput(String inputStr) {
		ThreadContext.put("myInput", inputStr);
		logger.error("userInput:{}", inputStr);
	}
}
